package com.ydj.app.entity;

import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.ydj.app.enumerate.MemberLevelEnum;
import com.ydj.framework.mysql.BaseEntity;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import java.util.Date;

@Getter
@Setter
@Accessors(chain = true)
@Entity
@Table(name = "web_user",
        uniqueConstraints = {
                @UniqueConstraint(name = "uni_openId", columnNames = "openId"),
                @UniqueConstraint(name = "uni_mobile", columnNames = "mobile"),
                @UniqueConstraint(name = "uni_email", columnNames = "email"),
        })
@org.hibernate.annotations.Table(appliesTo = "web_user", comment = "用户表")
public class WebUser extends BaseEntity {
    @Column(columnDefinition = "varchar(32) null comment '微信的OpenId'")
    private String openId;

    @Column(columnDefinition = "varchar(32) null comment '微信的UnionId'")
    private String unionId;

    @Column(columnDefinition = "varchar(50) comment '昵称'")
    private String nickName;

    @Column(columnDefinition = "varchar(32) null comment '手机号码'")
    private String mobile;

    @Column(columnDefinition = "varchar(100) null comment '邮箱'")
    private String email;

    @Column(columnDefinition = "varchar(50) comment '密码'")
    private String password;

    @Column(columnDefinition = "varchar(100) comment '邀请人ID'")
    private Long kolId;

    @Column(columnDefinition = "datetime comment '付费的有效期'")
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private Date expTime;

    @Column(columnDefinition = "tinyint comment '会员等级'", nullable = false)
    private MemberLevelEnum level;

    @Column(columnDefinition = "varchar(32) comment '最后登录的 IP'")
    private String lastLoginIp;

    @Column(columnDefinition = "datetime comment '最后登录时间'")
    private Date lastLoginTime;

    @Column(columnDefinition = "int comment '个人RWA预测免费测试次数剩余'")
    private Integer freeForecastPersonalCount;

    @Column(columnDefinition = "int comment '企业RWA预测免费测试次数剩余'")
    private Integer freeForecastCompanyCount;

    @Column(columnDefinition = "int comment '免费对话次数剩余'")
    private Integer freeChatCount;

    @Column(columnDefinition = "varchar(300) default '' comment '备注'")
    private String remark;

    @Column(columnDefinition = "tinyint default 0 comment '曾经成为会员'")
    private Boolean becomeMember;

    public WebUser incFreeForecastPersonalCount(int value) {
        this.freeForecastPersonalCount = this.freeForecastPersonalCount + value;
        return this;
    }

    public WebUser incFreeForecastCompanyCount(int value) {
        this.freeForecastCompanyCount = this.freeForecastCompanyCount + value;
        return this;
    }

    public WebUser incFreeChatCount(int value) {
        this.freeChatCount = this.freeChatCount + value;
        return this;
    }

    // 是否在有效期内的会员
    public boolean isMemberInExpTime() {
        return level.isMember() && expTime != null && expTime.after(new Date());
    }
}